﻿using DongFangGuoKai.WMS.IApiService;
using DongFangGuoKai.WMSPlatform.Commn;
using DongFangGuoKai.WMSPlatform.Commn.AppsetingsModule;
using DongFangGuoKai.WMSPlatform.Commn.HttpModule;
using DongFangGuoKai.WMSPlatform.Commn.HttpModule.JsonFormat.NewtonSoft;
using DongFangGuoKai.WMSPlatform.Domain.Enums;
using DongFangGuoKai.WMSPlatform.Domain.QueceModels;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using RestSharp;
using RestSharp.Serializers.NewtonsoftJson;

namespace DongFangGuoKai.WMS.ApiService
{
    /// <summary>
    /// 叫号系统接口
    /// </summary>
    public class QueceSystemService : IQueceSystemService
    {

        public string RequstUrl;
        public QueceSystemService(IServiceProvider serviceProvider)
        {
            RequstUrl = ConfigManageHelper.NumberSystemServicePath;

            //请求设置
            RestClientOptions options = new RestClientOptions();
            //请求超时时间
            options.MaxTimeout = 60 * 1000;

            JsonSerializerSettings DefaultSettings = new JsonSerializerSettings
            {
                ContractResolver = new ToLowerPropertyNamesContractResolver(),
                DefaultValueHandling = DefaultValueHandling.Include,
                TypeNameHandling = TypeNameHandling.None,
                NullValueHandling = NullValueHandling.Ignore,
                Formatting = Formatting.None,
                ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor
            };

            ConfigureSerialization serialConfig = s => s.UseNewtonsoftJson(DefaultSettings);

            RestClient restClient = ActivatorUtilities.CreateInstance<RestClient>(serviceProvider, options, serialConfig);

            Client = ActivatorUtilities.CreateInstance<CtmHttpClient>(serviceProvider, restClient);
        }
        public ICtmHttpClient Client { get; }

        /// <summary>
        /// 插队入队
        /// </summary>
        public QueceResultRespone AddQuece(AddQueceModel addQueceModel)
        {
            var url = BulidRequstUrl(QueceRequestType.AddQuece);
            return Retry.Execute(() =>
             {
                 return Client.Post<QueceResultRespone>(url, addQueceModel);
             }, TimeSpan.FromSeconds(3));
        }

        private string BulidRequstUrl(QueceRequestType queceRequestType)
        {
            switch (queceRequestType)
            {
                //插队入队
                case QueceRequestType.AddQuece:
                    return RequstUrl + "/queueSysinterface.aspx";

                default:
                    break;
            }
            return "";
        }






    }
}
